package com.example.mobile2030606162zwh0615;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

public class UsersRViewAdapter extends RecyclerView.Adapter<UsersRViewAdapter.ViewHolder> {
    //    4、定义列表数据，并在构造方法中赋值
    private List<Users> list;
    public UsersRViewAdapter(List<Users> list) {
        this.list = list;
    }
    //    1、定义ViewHolder静态内部类，继承RecyclerView.ViewHolder，用于描述列表项控件布局
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvItemId,tvItemName,tvItemAge,tvItemScore;
        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            tvItemId = itemView.findViewById(R.id.tvItemId);
            tvItemName = itemView.findViewById(R.id.tvItemName);
            tvItemAge = itemView.findViewById(R.id.tvItemAge);
            tvItemScore = itemView.findViewById(R.id.tvItemScore);
        }
    }
    //    3、添加需要实现的抽象方法
    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
//        6、获取列表布局文件，然后创建ViewHolder对象
        View view= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_rv_main_user,parent,false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull @NotNull UsersRViewAdapter.ViewHolder holder, int position) {
//     7、获取数据，并赋值给列表中的控件
        Users users = list.get(position);
        holder.tvItemId.setText(String.valueOf(users.get_id()));
        holder.tvItemName.setText(users.getName());
        holder.tvItemAge.setText(String.valueOf(users.getAge()));
        holder.tvItemScore.setText(String.valueOf(users.getScore()));
    }
    @Override
    public int getItemCount() {
        return list.size();//5、获取数据长度
    }
}
